package com.liyghting.txtsplit;

import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

/**
 * @author LiYiGuang
 * @date 2021/1/8 15:37
 */
public class TxtSplit {
	public static void main(String[] args) throws Exception {
		String txtPath = "D:\\user\\01370744\\Downloads\\temp.txt";
		// String txtPath = "D:\\user\\01370744\\Downloads\\1.txt";
		Path path = Paths.get(txtPath);
		long size = Files.size(path);
		System.out.println(size + "date" + new Date());
		long houserend = 1024L * 1024 * 100;
		byte[] bytes = Files.readAllBytes(path);
		long currentTimeMillis = System.currentTimeMillis();
		String smallFile = "D:\\user\\01370744\\Downloads\\" + currentTimeMillis + "new.txt";
		Path smallPath = Paths.get(smallFile);
		Files.createFile(smallPath);
		OutputStream outputStream = Files.newOutputStream(smallPath);
		byte[] temp = new byte[4096];
		int length = 4096;
		int writedSize = 0;
		int writedFileCount = 0;
		for (int i = 0; i < bytes.length;) {
			if (i + 4096 > bytes.length) {
				length = bytes.length - i;
				temp = new byte[length];
			}
			System.arraycopy(bytes, i, temp, 0, length);
			outputStream.write(temp);
			writedSize = writedSize + length;
			if (writedSize > houserend) {
				writedFileCount++;
				smallFile = "D:\\user\\01370744\\Downloads\\" + currentTimeMillis + "new" + writedFileCount + ".txt";
				smallPath = Paths.get(smallFile);
				Files.createFile(smallPath);
				outputStream = Files.newOutputStream(smallPath);
				writedSize = 0;
			}
			if (i + 4096 > bytes.length) {
				break;
			} else {
				i = i + 4096;
			}

		}
		System.out.println("date" + new Date());

	}
}
